package com.party.controller.sys.vote;

import com.party.common.Page;
import com.party.model.SysUser;
import com.party.model.VoPtion;
import com.party.model.VotePerson;
import com.party.service.excellentparty.VotePersonService;
import com.party.service.sys.SysUserService;
import com.party.service.vote.VoPtionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/sys/voption")
public class VoptionController {

    @Resource
    VotePersonService votePersonService;

    @Resource
    SysUserService sysUserService;

    @Resource
    VoPtionService voPtionService;

    private int pageSize = 9;

    /**
     * 首页
     * @return
     */
    @RequestMapping(value = "/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/sys/vote/byvotepersonlist");
        return modelAndView;
    }

    /**
     * 查询数据
     * @param req
     * @return
     */
    @RequestMapping(value = "/query")
    @ResponseBody
    public Map<String,Object> query(HttpServletRequest req) {
        Map<String, Object> map = new HashMap<String, Object>();
        String pageIndexStr = req.getParameter("pageIndex");
        String title = req.getParameter("title");
        String votename = req.getParameter("votename");

        int pageIndex = 1;
        if (pageIndexStr != null && !"".equals(pageIndexStr)) {
            pageIndex = Integer.parseInt(pageIndexStr);
        }

        VoPtion voPtion = new VoPtion();
        voPtion.setTitle(title);
        voPtion.setVotename(votename);
        Page p = this.voPtionService.getVoPtionList(voPtion, pageSize, pageIndex);

        map.put("pageCount",p.getTotalPageCount());
        map.put("tableData",p.getResult());
        return map;
    }

    /**
     * 获取所有人
     * @return
     */
    @RequestMapping(value = "/allPersons")
    public ModelAndView voteParty(HttpServletRequest request) {
        String voteid=request.getParameter("voteid");
        ModelAndView modelAndView = new ModelAndView();
        List<VoPtion> listUser=voPtionService.getAllVoPtionByVoteId(Integer.parseInt(voteid));
        modelAndView.addObject("listUser",listUser);
        modelAndView.setViewName("/sys/vote/addSelectVotePerson");
        modelAndView.addObject("voteid", voteid);
        return modelAndView;
    }

    /**
     * 添加投票人员
     * @return
     */
    @RequestMapping(value = "/addVoption")
    public ModelAndView editVotePerson(
                                       @RequestParam(value = "voteid")Integer voteid,
                                       @RequestParam(value = "title")String title,
                                       @RequestParam(value = "url")String url,
                                       @RequestParam(value = "content")String content,
                                       HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        VoPtion voption=new VoPtion();
        String msg="";

        voption.setVoteid(voteid);
        voption.setTitle(title);
        voption.setUrl(url);
        voption.setContent(content);
        int num=voPtionService.addVoPtion(voption);
        if(num>0){
            msg="添加成功";
        }else{
            msg="添加失败";
        }

        List<VoPtion> listUser=voPtionService.getAllVoPtionByVoteId(voteid);

        modelAndView.addObject("listUser",listUser);

        modelAndView.addObject("voteid",voteid);
        modelAndView.addObject("msg",msg);

        modelAndView.setViewName("/sys/vote/addSelectVotePerson");
        return modelAndView;
    }

    /**
     * 删除
     *
     * @param ids
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Map<String, Object> delete(
            @RequestParam("ids") String ids,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {

        int num = 0;
        try {
            num = voPtionService.deleteVoPtionByIds(ids);

        } catch (Exception e) {
            num = -1;
            e.printStackTrace();
        }
        Map<String, Object> modelMap = new HashMap<String, Object>();
        modelMap.put("num", num);
        return modelMap;
    }
}
